/**
 * ...
 * @author nado
 * @author Alex Malygin
 */

package ;


import haxe.Http;
import php.db.Manager;
import db.Item;
import db.Channel;


class RSS {
	
	private	var url				: String;
	private var xml_channel		: Xml;
	private var channel_id		: Int;
	
	public function new(url: String) 
	{	
		this.url = url;
		try 
		{
			var str		 = Http.requestUrl(url);
			xml_channel  = Xml.parse(str).firstElement().firstElement();
		}catch (e:Dynamic) 
		{
			trace(e);
			return; 
		}
	}
	
	public function setChannel_id(channel_id : Int)
	{
			this.channel_id = channel_id;
	}
	
	private function myNodeValue(node : Xml):String
	{
		var str	: String = "";
		
		if (node.firstChild() != null) 
			for (x in node) { str += x.nodeValue;}
		return str;
	}
	
	private function info(info : String, iterator : Xml)
	{	
		var temptext : String = "";
		for (node in iterator.elementsNamed(info) )
			temptext = myNodeValue(node);
		return temptext;
	}
	public function getChannel():Channel
	{
		var channel			: Channel;
		var title			: String ;
		var link			: String ;
		var link_img		: String = "";
		var description		: String ;
		var date			: Date 		= Date.now();
			
		title           = info("title", xml_channel);
		link 			= info("link", xml_channel);
		description     = info("description", xml_channel);
			
		for (node in xml_channel.elementsNamed("image") ) {
			link_img = info("url", node);
		}	
		
		if ((title != "") && (link != ""))
		{
			channel = new Channel(title, link, date);
			if (description != "") 	{channel.description = description;}
			if (link_img != "") { channel.link_img = link_img; }
			else { channel.link_img = "http://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/100px-Feed-icon.svg.png"; }
			channel.update;
		}
		else { throw new String ("something wrong in channel"); }
		return channel;	
	}
	
	public function getItems():Array<Item>
	{
		var newItem          : Item;
		var items			 = new Array<Item>();
		var DefautCat		 = new Array<String>(); DefautCat.push("");
		var categories		 = new Array<String>();
		var title			 : String = "";
		var link			 : String = ""; 
		var description      : String = "";
		var date			 : String = "";
		var author			 : String = ""; 
		
		for(item in xml_channel.elementsNamed("item") ) 
		{ 
			if (channel_id == null) throw new String("you have to set channel id");
			categories = new Array<String>();
			
			title 				= info("title", item);
			link  				= info("link", item);
			description		    = info("description", item);
			date			    = info("pubDate", item);
			author			    = info("author", item);
			categories.push(info("category", item));
			
			if ((title != "") && (link != ""))// || (date != ""))
			{
				newItem = new Item(title, link, Date.now() ,channel_id);
				if (author != "") newItem.author = author; 
				if (description != "") newItem.description = description;
				if (categories.length != 0) 	 
				 {newItem.category = categories; }
				else { newItem.category = DefautCat; } 
				items.push(newItem);
			}
			else { throw new String ("something wrong in item"); }
		}
		return items;
	}  
}